[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr MapViewOfFile(
IntPtr hFileMappingObject,
FileMapAccess dwDesiredAccess,
UInt32dwFileOffsetHigh,
UInt32dwFileOffsetLow,
UIntPtr dwNumberOfBytesToMap);
[Flags]
public enum FileMapAccess : uint
{
FileMapCopy = 0x0001,
FileMapWrite = 0x0002,
FileMapRead = 0x0004,
FileMapAllAccess = 0x001f,
FileMapExecute = 0x0020,
}
Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As IntPtr, ByVal dwDesiredAccess As UInt32, ByVal dwFileOffsetHigh As UInt32, ByVal dwFileOffsetLow As UInt32, ByVal dwNumberOfBytesToMap As UInt32) As UInt32
Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As IntPtr, ByVal dwDesiredAccess As UInt32, ByVal dwFileOffsetHigh As UInt32, ByVal dwFileOffsetLow As UInt32, ByVal dwNumberOfBytesToMap As UInt32) As UInt32
Public Structure SECURITY_ATTRIBUTES
Public lLength As Long
Public lSecurityDescriptor As Long
Public lInhertitHandle As Long
End Structure
Dim SecAttrib As SECURITY_ATTRIBUTES
Dim nAckEvent As Integer
Dim nReadyEvent As Integer
Dim nSharedFile As Long
Dim nSharedMem As Long
Dim nSecurityDesc As Integer
Public Const GPTR As Long = &H40
Public Const SECURITY_DESCRIPTOR_REVISION As Long = (1)
Public Const SECURITY_DESCRIPTOR_MIN_LENGTH As Long = (20)
Public Const PAGE_READWRITE As Int32 = &H4
Public Const FILE_MAP_READ As Int32 = &H40&, 512)
nSecurityDesc = GlobalAlloc(GPTR, SECURITY_DESCRIPTOR_MIN_LENGTH)
SecAttrib.lLength = SecAttrib.nLength
SecAttrib.lInheritHandle = Convert.ToInt32(True)
SecAttrib.lSecurityDescriptor = pSecurityDesc
nAckEvent = CreateEvent(SecAttrib, 0, 0, "DBWIN_BUFFER_READY")
nReadyEvent = CreateEvent(SecAttrib, 0, 0, "DBWIN_DATA_READY")
nSharedFile = CreateFileMapping(-1, SecAttrib, PAGE_READWRITE, 0&, 4096, "DBWIN_BUFFER")
nSharedMem = MapViewOfFile(nSharedFile, FILE_MAP_READ, 0&, 0&, 0&, 512)
None.
#define SECTION_QUERY 0x0001
#define SECTION_MAP_WRITE 0x0002
#define SECTION_MAP_READ 0x0004
#define SECTION_MAP_EXECUTE 0x0008
#define SECTION_EXTEND_SIZE 0x0010
#define SECTION_MAP_EXECUTE_EXPLICIT 0x0020 // not included in SECTION_ALL_ACCESS
#define SECTION_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SECTION_QUERY|\
SECTION_MAP_WRITE | \
SECTION_MAP_READ | \
SECTION_MAP_EXECUTE | \
SECTION_EXTEND_SIZE)
#define FILE_MAP_COPY SECTION_QUERY
#define FILE_MAP_WRITE SECTION_MAP_WRITE
#define FILE_MAP_READ SECTION_MAP_READ
#define FILE_MAP_ALL_ACCESS SECTION_ALL_ACCESS
#define FILE_MAP_EXECUTE SECTION_MAP_EXECUTE_EXPLICIT // not included in FILE_MAP_ALL_ACCESS
Please add some!
const UInt32 STANDARD_RIGHTS_REQUIRED = 0x000F0000;
const UInt32 SECTION_QUERY = 0x0001;
const UInt32 SECTION_MAP_WRITE = 0x0002;
const UInt32 SECTION_MAP_READ = 0x0004;
const UInt32 SECTION_MAP_EXECUTE = 0x0008;
const UInt32 SECTION_EXTEND_SIZE = 0x0010;
const UInt32 SECTION_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED|SECTION_QUERY|
SECTION_MAP_WRITE |
SECTION_MAP_READ |
SECTION_MAP_EXECUTE |
SECTION_EXTEND_SIZE);
const UInt32 FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;
private IntPtr hHandle;
private IntPtr pBuffer;
unsafe public void Attach(string SharedMemoryName, UInt32 NumBytes)
{
if (IntPtr.Zero != hHandle) return;
hHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, SharedMemoryName);
if (IntPtr.Zero == hHandle) return;
pBuffer = MapViewOfFile(hHandle, FILE_MAP_ALL_ACCESS, 0, 0, NumBytes);
}
public void Detach()
{
if (IntPtr.Zero != hHandle)
{
CloseHandle(hHandle); //fair to leak if can't close
hHandle = IntPtr.Zero;
}
pBuffer = IntPtr.Zero;
lBufferSize = 0;
}
Do you know one? Please contribute it!